<template>
  <el-dialog width="25%" :visible.sync="visible" title="导入数据">
    <el-upload
      class="upload-demo"
      drag
      action="https://jsonplaceholder.typicode.com/posts/"
      :on-success="handleUploadSuccess"
      :before-upload="beforeUpload"
      style="text-align: center"
    >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      <div class="el-upload__tip" slot="tip">只能上传文件，且不超过5M</div>
    </el-upload>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: ['visible'],
  data() {
    return {
      file: null
    }
  },
  methods: {
    beforeUpload(file) {
      // 文件上传前的验证逻辑
      console.log('文件上传前的验证', file);
      return true;
    },
    handleUploadSuccess(response, file, fileList) {
      // 文件上传成功后的处理逻辑
      console.log('文件上传成功', response, file, fileList);
      this.file = file
    },
    parseFile(file) {
      // 解析文件的逻辑
      console.log('解析文件', file);
      // 这里可以使用库如 xlsx 或 csv-parser 来解析文件内容
      // 返回解析后的数据
      return [{ title: '示例工单标题' }];
    },
    closeDialog() {
      this.$emit('update:visible', false);
    },
    submit() {
      // 解析文件并准备数据
      this.$emit('on-success', this.parseFile(this.file));
      this.closeDialog();
    }
  }
};
</script>
